<template>
  <div>
    <el-dialog :title="saveObj.title" :visible.sync="saveObj.show">
      <el-form
        ref="form"
        :rules="rules"
        :model="formData"
        label-width="108px"
        size="small"
        class="demo-ruleForm"
      >
        <el-form-item label="指标编码" prop="indexCode">
          <el-input v-model="formData.indexCode" placeholder="请输入指标编码" />
        </el-form-item>
        <el-form-item label="指标名称" prop="indexName">
          <el-input v-model="formData.indexName" placeholder="请输入指标名称" />
        </el-form-item>
        <el-form-item label="指标单位" prop="indexUnit">
          <el-input v-model="formData.indexUnit" placeholder="请输入指标单位" />
        </el-form-item>
        <el-form-item label="指标类型" prop="indexType">
          <el-select v-model="formData.indexType" placeholder="指标类型">
            <el-option v-for="item in indexOpts" :key="item.key" :label="item.label" :value="item.key" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeFn('form')">取 消</el-button>
        <el-button type="primary" @click="sumbitFn('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addIndexlib, editIndexlib } from '@/api/infoConfig/indexlib'

export default {
  props: {
    saveObj: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      indexOpts: [
        {
          key: 1,
          label: '化验'
        },
        {
          key: 2,
          label: '仪表'
        }
      ],
      formData: {},
      rules: {
        indexCode: [{ required: true, message: '此为必填项', trigger: 'blur' }]
      }

    }
  },
  methods: {
    // 赋值表单数据
    getFormFn() {
      this.formData = this.saveObj.data
    },
    // 关闭窗口
    closeFn(data) {
      this.saveObj.show = false
      this.$refs[data].resetFields()
    },
    // 表单验证
    sumbitFn(data) {
      this.$refs[data].validate(valid => {
        if (valid) {
          this.sumbitSureFn(this.formData)
        } else {
          this.$message.error('*为必填项')
        }
      })
    },
    // 提交
    sumbitSureFn(data) {
      // 编辑
      if (this.formData.indexUid) {
        editIndexlib(this.formData).then(res => {
          this.$notify({
            title: '成功',
            message: res.msg,
            type: 'success'
          })
          this.saveObj.show = false
          this.$emit('getFn')
        })
      } else {
        // 新增
        addIndexlib(this.formData).then(res => {
          this.$notify({
            title: '成功',
            message: res.msg,
            type: 'success'
          })
          this.saveObj.show = false
          this.$emit('getFn')
        })
      }
    }
  }
}
</script>
